﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Collections.ObjectModel;

namespace UpdateValueAssistance
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class UpdateValueAssistanceWnd : Window
    {

        //protected DctmServices.DctmMiddleServiceClient dctm = null;
        protected List<DctmServices.ValueAssistance> listOfValues = null;

        public UpdateValueAssistanceWnd()
        {

            InitializeComponent();
            Initialize();

        }

        protected void Initialize()
        {
            //dctm = new DctmServices.DctmMiddleServiceClient();
            //dctm.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

            loadValueAssistanceListFromRepository();
        }

        protected void loadValueAssistanceListFromRepository()
        {
            DctmServices.DctmMiddleServiceClient dctm = new UpdateValueAssistance.DctmServices.DctmMiddleServiceClient();
            dctm.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

            listOfValues = dctm.GetCurrentValueAssistanceList("type1", "v_test_attr1");

            if (null == listOfValues)
            {
                MessageBox.Show("NULL list of value assistance returned.");
                return;
            }

            if (listOfValues.Count() < 1)
            {
                MessageBox.Show("No values returned!");
            }
            else
            {
                foreach (DctmServices.ValueAssistance obj in listOfValues)
                {
                    UpdateValueAssistanceListBox.Items.Add("Display String=" + obj.DisplayString + ".  Value=" + obj.Value);
                }
            }


        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
        }

        private void UpdateValueAssistanceListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            String valueToEdit = null;
            String strToEdit = null;

            int selectedPair = UpdateValueAssistanceListBox.SelectedIndex;
            valueToEdit = listOfValues[selectedPair].Value;
            strToEdit = listOfValues[selectedPair].DisplayString;

            valueBox.Text = valueToEdit;
            displayBox.Text = strToEdit;


        }

        private void modifyButton_Click(object sender, RoutedEventArgs e)
        {
            int selectedPair = UpdateValueAssistanceListBox.SelectedIndex;
            listOfValues[selectedPair].Value = valueBox.Text;
            listOfValues[selectedPair].DisplayString = displayBox.Text;
            UpdateValueAssistanceListBox.Items[selectedPair] = "Display String=" + listOfValues[selectedPair].DisplayString + ".  Value=" + listOfValues[selectedPair].Value;
        }

        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            int selectedPair = UpdateValueAssistanceListBox.SelectedIndex;
            if (selectedPair < 0)
            {
                return;
            }

            if (null != listOfValues)
            {
                listOfValues.RemoveAt(selectedPair);
            }
            valueBox.Text = String.Empty;
            displayBox.Text = String.Empty;
            UpdateValueAssistanceListBox.Items.RemoveAt(selectedPair);
        }

        private void addButton_Click(object sender, RoutedEventArgs e)
        {
            DctmServices.ValueAssistance newPair = new UpdateValueAssistance.DctmServices.ValueAssistance();
            newPair.DisplayString = displayBox.Text;
            newPair.Value = valueBox.Text;

            if (null == listOfValues)
            {
                listOfValues = new List<UpdateValueAssistance.DctmServices.ValueAssistance>();
            }
            listOfValues.Add(newPair);
            UpdateValueAssistanceListBox.Items.Add("Display String=" + newPair.DisplayString + ".  Value=" + newPair.Value);
            
        }

        private void updateButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DctmServices.DctmMiddleServiceClient dctm = new UpdateValueAssistance.DctmServices.DctmMiddleServiceClient();
                dctm.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
                dctm.setValueAssistanceList(listOfValues, "type1", "v_test_attr1");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }

        }

    }
}
